package com.fc.v2.WX;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fc.v2.model.MapObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

public class WXService {

    /**
     * 获取微信平台证书
     */
    public static void getWXCertification(){
            HttpClient httpClient = WXUtil.getWXHttpClient();

        // 创建http请求(get方式)
            HttpGet httpget = new HttpGet("https://api.mch.weixin.qq.com/v3/certificates");
            httpget.setHeader("Accept","application/json");
            httpget.setHeader("User-Agent","用户代理(https://zh.wikipedia.org/wiki/User_agent)");
        // 后面跟使用Apache HttpClient一样
            try {
                HttpResponse response = httpClient.execute(httpget);
//                System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    /**
     * 统一下单
     */
    public static String putOrder(String orderId, String content, BigDecimal amount,String openId,MapObject attach) throws UnsupportedEncodingException {

        HttpClient httpClient = WXUtil.getWXHttpClient();
        int total = amount.multiply(new BigDecimal(100)).intValue();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appid", WXParam.APPID);
        jsonObject.put("mchid", WXParam.MERCHANTID);
        jsonObject.put("description",content);
        jsonObject.put("out_trade_no",orderId);
        jsonObject.put("notify_url", WXUrl.NOTIFY_URL);
        jsonObject.put("amount",new MapObject().put("total",total).put("currency","CNY"));
        jsonObject.put("payer",new MapObject().put("openid", openId));
        if(attach!=null){
            jsonObject.put("attach",JSONUtil.toJsonStr(attach));
        }
        System.out.println("添加微信订单：\n"+jsonObject.toStringPretty());

        StringEntity stringEntity = new StringEntity(jsonObject.toString());
        stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());

        // 创建http请求
        HttpPost httpPost = new HttpPost(WXUrl.PAY);
        httpPost.setEntity(stringEntity);
        httpPost.setHeader("Accept","application/json");
        httpPost.setHeader("User-Agent","用户代理(https://zh.wikipedia.org/wiki/User_agent)");
        // 后面跟使用Apache HttpClient一样
        try {
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            JSONObject resJson = JSONUtil.parseObj(EntityUtils.toString(entity));
            if(resJson.get("prepay_id")==null){
                throw new RuntimeException("生存微信订单出错:"+resJson.toString());
            }else{
                return resJson.get("prepay_id").toString();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static JSONObject callMiniPay(String prepayId){
        String timeStamp = String.valueOf( DateUtil.currentSeconds());
        String nonceStr = RandomUtil.randomString(32);
        String packageStr = "prepay_id=" + prepayId;

        String paySign = WXUtil.getMiniProgramSign(WXParam.APPID, timeStamp, nonceStr, packageStr);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId", WXParam.APPID);
        jsonObject.put("timeStamp",timeStamp);
        jsonObject.put("nonceStr",nonceStr);
        jsonObject.put("package",packageStr);
        jsonObject.put("signType","RSA");
        jsonObject.put("paySign",paySign);
        return jsonObject;
    }


}
